import axios from "axios";
// 引入element ui组件
// 引入路由文件
import Cookies from "js-cookie";
import NProgress from "nprogress";

axios.defaults.headers.post["Content-Type"] =
  "application/x-www-form-urlencoded";

const request = axios.create({
  timeout: 5000,
});
// 请求拦截
request.interceptors.request.use(
  (config) => {
    NProgress.start();
    config.headers["token"] = Cookies.get("token");
    // 设置请求头
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
request.interceptors.response.use(
  (response) => {
    NProgress.done();
    return response.data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default request;
